package com.elinkway.infinitemovies.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.NewAdDataBean;
import com.elinkway.infinitemovies.bean.RelationAlbum;
import com.elinkway.infinitemovies.bean.RelationList;
import com.elinkway.infinitemovies.bean.SDKPayload;
import com.elinkway.infinitemovies.bean.TtAdBean;
import com.elinkway.infinitemovies.bean.TtPayload;
import com.elinkway.infinitemovies.selfdata.SelfDataConstant;
import com.elinkway.infinitemovies.utils.MoviesConstant;
import com.elinkway.infinitemovies.utils.Utils;
import com.elinkway.infinitemovies.view.BaiduNativeAdView;
import com.elinkway.infinitemovies.view.GdtNativeAdView;
import com.elinkway.infinitemovies.view.TtPayAdView;
import com.le123.ysdq.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by biandi on 2017/4/27.
 */

public class RelativeListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private RelationList mRelationList;
    private Activity mContext;
    private RelativItemClickListener mListener;
    private NewAdDataBean mRecomBeanFirst; // 推荐1广告数据
    private NewAdDataBean mRecomBeanSecond; // 推荐2广告数据
    private BaiduNativeAdView mBaiduAd;

    public enum ITEM_TYPE {
        AD, // 广告
        NORMAL // 推荐
    }

    public RelativeListAdapter(Activity context,RelationList list,RelativItemClickListener listener) {
        mRelationList = list;
        mContext = context;
        mListener = listener;
    }

    public void setRecomBean(NewAdDataBean bean, String recomPos) {
        if (SelfDataConstant.AD_PO_RECOM1.equals(recomPos)) {
            mRecomBeanFirst = bean;
        } else if (SelfDataConstant.AD_PO_RECOM2.equals(recomPos)) {
            mRecomBeanSecond = bean;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if (viewType == ITEM_TYPE.AD.ordinal()) {
            // 广告
            if (mContext != null) {
                holder = new AdViewHolder(LayoutInflater.from(mContext).
                        inflate(R.layout.relative_list_ad_item, parent, false));
            }
        } else if (viewType == ITEM_TYPE.NORMAL.ordinal()) {
            // 推荐
            if (mContext != null) {
                holder = new RelativeViewHolder(LayoutInflater.from(mContext).
                        inflate(R.layout.relative_list_item, parent, false), mListener);
            }

        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewholder, int position) {
        if (mRelationList == null || getItemCount() == 0 || viewholder == null) {
            return;
        }
        if (viewholder instanceof AdViewHolder) {
            if (mRecomBeanFirst != null && position == mRecomBeanFirst.ext.pos) {
                setAdData(mRecomBeanFirst,viewholder, SelfDataConstant.AD_PO_RECOM1);
            } else if (mRecomBeanSecond != null && position == mRecomBeanSecond.ext.pos) {
                setAdData(mRecomBeanSecond,viewholder, SelfDataConstant.AD_PO_RECOM2);
            }
        } else if (viewholder instanceof RelativeViewHolder) {
            // 推荐
            RelativeViewHolder holder = (RelativeViewHolder)viewholder;
            RelationAlbum info = mRelationList.get(position);
            if (info == null) {
                return;
            }
            String strDesc = "";
            // 图片
            if (!TextUtils.isEmpty(info.getPic())) {
                ImageLoader.getInstance().displayImage(info.getPic(),
                        holder.imgPic);
            }
            // 分数
            if (!TextUtils.isEmpty(info.getScore())) {
                strDesc = info.getScore()+mContext.getResources().getString(R.string.score);
            }
            // 角标
            String mark = "";
            if (MoviesConstant.VT_MOVIE.equals(info.getVt())) {
                // 电影显示分数
                mark = strDesc;
            } else if (MoviesConstant.VT_TV.equals(info.getVt()) || MoviesConstant.VT_CARTOON.equals(info.getVt())){
                // 电视剧、动漫
                if ("1".equals(info.getIsEnd())) {
                    // 完结
                    mark = info.getEpisodes();
                    if (!TextUtils.isEmpty(mark)) {
                        mark = mark + mContext.getResources().getString(R.string.episode_total);
                    }
                } else {
                    // 未完结
                    mark = info.getNowEpisode();
                    if (!TextUtils.isEmpty(mark)) {
                        mark = mContext.getResources().getString(R.string.updateto) + mark + mContext.getResources().getString(R.string.episode);
                    }
                }

            } else if (MoviesConstant.VT_ZONGYI.equals(info.getVt()) || MoviesConstant.VT_DOCUMENTARY.equals(info.getVt())) {
                // 综艺、纪录片
                if ("1".equals(info.getIsEnd())) {
                    // 完结
                    mark = info.getEpisodes();
                    if (!TextUtils.isEmpty(mark)) {
                        mark = mContext.getString(R.string.end_episodes_zongyi,mark);
                    }
                } else {
                    // 未完结
                    mark = info.getNowEpisode();
                    if (!TextUtils.isEmpty(mark)) {
                        mark = mContext.getString(R.string.updating_episodes_zongyi,mark);
                    }
                }
            }
            if (!TextUtils.isEmpty(mark)) {
                holder.textScore.setText(mark);
                holder.textScore.setVisibility(View.VISIBLE);
            } else {
                holder.textScore.setVisibility(View.GONE);
            }
            // 标题
            if (!TextUtils.isEmpty(info.getTitle())) {
                holder.textTitle.setText(info.getTitle());
            }
            // 年
            if (!TextUtils.isEmpty(info.getYear())) {
                if (!TextUtils.isEmpty(strDesc)) {
                    strDesc = strDesc + "·" + info.getYear()+mContext.getString(R.string.year);
                } else {
                    strDesc = info.getYear()+mContext.getString(R.string.year);
                }
            }
            // 类型
            if (!TextUtils.isEmpty(info.getSubCategory())) {
                if (!TextUtils.isEmpty(strDesc)) {
                    strDesc = strDesc + "·" + info.getSubCategory();
                } else {
                    strDesc = info.getSubCategory();
                }
            }
            // 地域
            if (!TextUtils.isEmpty(info.getArea())) {
                if (!TextUtils.isEmpty(strDesc)) {
                    strDesc = strDesc + "·" + info.getArea();
                } else {
                    strDesc = info.getArea();
                }
            }
            // 描述
            if (!TextUtils.isEmpty(strDesc)) {
                holder.textDesc.setText(strDesc);
            }
        }


    }

    public void setAdData(NewAdDataBean bean,RecyclerView.ViewHolder viewHolder, String recomPos) {
        if (bean == null) {
            return;
        }
        String adId = null;
        if (Utils.BAIDU_AD.equals(bean.provider) || Utils.GDT_AD.equals(bean.provider)) {
            adId = ((SDKPayload)bean.payload).adId;
        }
        if (Utils.BAIDU_AD.equals(bean.provider) && !TextUtils.isEmpty(adId)) {
            // 百度
            mBaiduAd = new BaiduNativeAdView(mContext, ((AdViewHolder) viewHolder).view, recomPos);
            mBaiduAd.setBaiduAd(adId);
            EventBus.getDefault().register(mBaiduAd);
        } else if (Utils.GDT_AD.equals(bean.provider) && !TextUtils.isEmpty(adId)) {
            // 广点通
            GdtNativeAdView gdtAd = new GdtNativeAdView(mContext, "recom", ((AdViewHolder) viewHolder).view,  recomPos);
            gdtAd.loadAD(adId);
        } else if (Utils.TT_AD.equals(bean.provider)) {
            TtPayAdView ttPayAdView = new TtPayAdView(mContext, ((AdViewHolder) viewHolder).view, recomPos);
            ttPayAdView.setTtAd(((TtPayload) bean.payload).ad);
        }


    }

    @Override
    public int getItemViewType(int position) {
        if (mRecomBeanFirst != null && position == mRecomBeanFirst.ext.pos ||
                mRecomBeanSecond != null && position == mRecomBeanSecond.ext.pos) {
            return ITEM_TYPE.AD.ordinal();
        } else {
            return ITEM_TYPE.NORMAL.ordinal();
        }
    }

    @Override
    public int getItemCount() {
        if (mRelationList != null) {
            return mRelationList.size();
        }
        return 0;
    }


    public class RelativeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView textScore;
        private TextView textTitle;
        private TextView textDesc;
        private ImageView imgPic;
        private RelativItemClickListener mListener;

        public RelativeViewHolder(View view,RelativItemClickListener listener) {
            super(view);
            textScore = (TextView) view.findViewById(R.id.text_reco_score);
            textTitle = (TextView) view.findViewById(R.id.text_reco_title);
            textDesc = (TextView) view.findViewById(R.id.text_reco_desc);
            imgPic = (ImageView) view.findViewById(R.id.img_reco_pic);
            mListener = listener;
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if(mListener != null){
                mListener.onRelativeItemClick(v,getAdapterPosition());
            }
        }
    }

    public class AdViewHolder extends RecyclerView.ViewHolder {
        public View view;



        public AdViewHolder(View itemView) {
            super(itemView);
            view = itemView.findViewById(R.id.relative_item_ad);
        }
    }

    public interface RelativItemClickListener {
        void onRelativeItemClick(View view, int postion);
    }

    public class RecomAdBean {
        public String mAdType; // 广告类型 广点通or百度
        public int mAdPos = -1; // 广告位置
        public String mAdId; // 广告id
        public String mRecomPos; // 推荐位置

        public RecomAdBean(int pos,String adType,String adId,String recomPos) {
            mAdPos = pos;
            mAdType = adType;
            mAdId = adId;
            mRecomPos = recomPos;
        }
    }
}
